// pages/showM/showM.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  
  data: {
    orders: [],
    contentlist: [],
    topHeight: 0,
    mtr: "",
    pages: 1,
    pageNum: 20,
    totalCount: 0,
    hide: false,
    hideview: "main_mid_view_hide",
    loadimg: "../images/arrow_down.png",
    elementlist: [{ element: "", symbol1: "", eletratio1: "", symbol2: "", eletratio2: "", deleteimg: "../images/delete.png" }],
    whtlist: [{ symbol: "", whtratio: "", deleteimg: "../images/delete.png" }],
    dept: null

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: (res) => {
        that.setData({
          topHeight: res.windowHeight
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  bindconfirm: function (e) {
    this.setData({
      mtr: e.detail.value
    });
    this.BindWXMtrOrderSearch();
  },

  mtrInputFun: function (e) {
    this.setData({
      mtr: e.detail.value
    });
  },
  BindSaoYiSao:function(){
    var tel = app.globalData.tel;
    if (tel === null) {
      wx.navigateTo({
        url: '../logins/logins',
      })
    } else {
      wx.scanCode({
        success: (res) => {
          var codearr = res.result.replace(/\r\n/g, "|-&");
          var code = codearr.split('|-&')[0].split(':')[1];
          // console.log(code)
          if (code != undefined && code != "" && code != null) {
            wx.request({
              url: app.wxlocurl.url + 'WXLogin/WXGetMtrSaoYiSao',
              header: { "Content-Type": "application/json" },
              data: {
                tel: tel,//全局的手机号
                code: code
              },
              method: "POST",
              success: (res)=> {
                if (res.data.length == 0) {
                  wx.showModal({
                    title: '提示',
                    content: '二维码未扫描出内容',
                    success: function (res) {
                      if (res.errMsg == "showModal:ok") {
                        wx.navigateTo({
                          url: 'pages/Search_Order/Search_Order', 
                          success: function (e) {
                            var page = getCurrentPages().pop(); //当前页面
                            if (page == undefined || page == null) return;
                            page.onLoad(); //或者其它操作
                          }
                        });
                      }
                    }
                  })
                }
                var mtrdata = res.data;
                var numed;
                var deptd;
                for (var i = 0; mtrdata.length; ++i) {
                  numed = mtrdata[0].Isinsider;
                  deptd = mtrdata[0].dept;
                  break;
                }
                getApp().globalData.inisder = numed;
                getApp().globalData.dept = deptd;
                this.setData({
                  orders: res.data,
                  dept: deptd
                })
              }
            })
          }
        },
      })
    }
  },

  BindWXMtrOrderSearch: function () {
    var phoneNumber = getApp().globalData.tel;
    var deptd = getApp().globalData.dept;
    this.setData({
      dept : getApp().globalData.dept
    });
    
    wx.request({
      url: app.wxlocurl.url + 'WXLogin/WXGetOrderList',
      data: {
        type: 5,
        tel: phoneNumber,//全局的手机号
        orderNumber: this.data.mtr,//订单号
        dept: deptd
      },
      method: "POST",
      contentType: "application/x-www-form-urlencoded;charset=utf-8",
      success: (res) => {
        this.setData({
          orders: res.data
        })
      },
      fail: () => {
        wx.showToast({
          title: '查询失败',
          icon: 'success',
          duration: 2000
        });
      }
    })
   
  },

  BindLoadMore: function (e) {
    var that = this;
    if (that.data.hide == false) {
      that.data.hide = true;
      that.data.hideview = "main_mid_view_show";
      that.data.loadimg = "../images/up_arrow.png";
    } else {
      that.data.hide = false;
      that.data.hideview = "main_mid_view_hide";
      that.data.loadimg = "../images/arrow_down.png";
      that.data.elementlist = [{ element: "", symbol1: "", eletratio1: "", symbol2: "", eletratio2: "", deleteimg: "../images/delete.png" }];
      that.data.whtlist = [{ symbol: "", whtratio: "", deleteimg: "../images/delete.png" }];
    }
    that.setData({
      hideview: that.data.hideview,
      loadimg: that.data.loadimg,
      elementlist: that.data.elementlist,
      whtlist: that.data.whtlist,
    })
  },

  BindElementAdd: function (e) {
    var that = this;
    var data = { element: "", symbol1: "", eletratio1: "", symbol2: "", eletratio2: "", deleteimg: "../images/delete.png" };
    that.data.elementlist.push(data);
    that.setData({
      elementlist: that.data.elementlist,
    })
  },

  BindElementDel: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;//获取data-index
    that.data.elementlist.splice(index, 1);
    that.setData({
      elementlist: that.data.elementlist,
    })
  },

  elementInputFun: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.elementlist[index].element = e.detail.value;
  },
  symbolInputFun: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.elementlist[index].symbol1 = e.detail.value;
  },
  eletratioInputFun: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.elementlist[index].eletratio1 = e.detail.value;
  },
  symbolInputTwoFun: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.elementlist[index].symbol2 = e.detail.value;
  },
  eletratioInputTwoFun: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.elementlist[index].eletratio2 = e.detail.value;
  },

  BindWhtAdd: function (e) {
    var that = this;
    var data = { symbol: "", whtratio: "", deleteimg: "../images/delete.png" };
    that.data.whtlist.push(data);
    that.setData({
      whtlist: that.data.whtlist,
    })
  },
  BindWhtDel: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;//获取data-index
    that.data.whtlist.splice(index, 1);
    that.setData({
      whtlist: that.data.whtlist,
    })
  },
  whtsymbolInputFun: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.whtlist[index].symbol = e.detail.value;
  },
  whtratioInputFun: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    that.data.whtlist[index].eletratio = e.detail.value;
  },
  // 点击 加入购物车
  handleCartAdd(e) {
    var DataGood = e.currentTarget.dataset.index;
    // 1 获取缓存中的购物车 数组
    let cart = wx.getStorageSync("cart") || [];
    // 2 判断 商品对象是否存在于购物车数组中
    let index = cart.findIndex(v => v.ID === DataGood.ID);
    if (index === -1) {
      //3  不存在 第一次添加
      DataGood.num = 1;
      DataGood.checkeds = true;
      cart.push(DataGood);
    } else {
      // 4 已经存在购物车数据 执行 num++
      cart[index].num++;
    }
    // 5 把购物车重新添加回缓存中
    wx.setStorageSync("cart", cart);
    // 6 弹窗提示
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      // true 防止用户 手抖 疯狂点击按钮 
      mask: true
    });
  },
  //退货
  handleTuiHuo(e) {
    var ID = e.currentTarget.dataset.index;
    wx.request({
      url: app.wxlocurl.url + 'WXLogin/WXSetTuiHuo?ID=' + ID,
      method: "POST",
      contentType: "application/x-www-form-urlencoded;charset=utf-8",
      success: (res) => {
        wx.showToast({
          title: '归还成功',
          icon: 'success',
          // true 防止用户 手抖 疯狂点击按钮 
          mask: true
        });
        this.BindWXMtrOrderSearch();
      },
      fail: () => {
        wx.showToast({
          title: '归还失败',
          icon: 'success',
          duration: 2000
        });
      }
    })
  },
  // 点击轮播图 放大预览
  handlePrevewImage(e) {
    var imgArr = e.currentTarget.dataset.list;
    var imged = [];
    var datas = this.data.orders;
    for (var i = 0; i < datas.length; i++) {
      imged.push(app.wxImageUrl.url + datas[i].img);
    }
    wx.previewImage({
      urls: imged,
      current: imgArr,
    });

  },

})